using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace MUSA
{
    class Tank
    {
        protected Vector3 position;
        protected Vector3 velocity;
        protected BoundingBox bb;
        protected Vector3 lastPosition;
        public void MoveForward()
        {
            lastPosition = position;
            position += velocity;
            bb.Min += velocity;
            bb.Max += velocity;
        }
        public void Backup()
        {
            position -= velocity;
            bb.Min -= velocity;
            bb.Max -= velocity;
        }
        public void ReverseVelocity()
        {
            velocity.X = -velocity.X;
        }

        protected VertexDeclaration decl;
        protected VertexBuffer vb;
        protected VertexPositionColor[] vpc;

        protected GraphicsDeviceManager graphics;
        protected ContentManager contentManager;
        protected Effect effect;

        public Tank(GraphicsDeviceManager nGraphics, ContentManager nContent)
        {
            graphics = nGraphics;
            contentManager = nContent;
        }

        public void Initialize()
        {
            position = new Vector3(5, 0, 0);
            bb = new BoundingBox(new Vector3(5, 0, 0), new Vector3(6, 1, 1));
            velocity = new Vector3(-0.1f, 0, 0);
        }

        public void LoadGraphicsContent()
        {
            //load effect
            effect = contentManager.Load<Effect>("Simple");


            //declare vertex and vertexbuffer
            GraphicsDevice device = graphics.GraphicsDevice;
            decl = new VertexDeclaration(device, VertexPositionColor.VertexElements);
            vb = new VertexBuffer(device, typeof(VertexPositionColor), 36, ResourceUsage.WriteOnly, ResourceManagementMode.Automatic);

            #region Define Cube

            Vector3 topLeftFront = new Vector3(-1.0f, 1.0f, 1.0f);
            Vector3 bottomLeftFront = new Vector3(-1.0f, -1.0f, 1.0f);
            Vector3 topRightFront = new Vector3(1.0f, 1.0f, 1.0f);
            Vector3 bottomRightFront = new Vector3(1.0f, -1.0f, 1.0f);
            Vector3 topLeftBack = new Vector3(-1.0f, 1.0f, -1.0f);
            Vector3 topRightBack = new Vector3(1.0f, 1.0f, -1.0f);
            Vector3 bottomLeftBack = new Vector3(-1.0f, -1.0f, -1.0f);
            Vector3 bottomRightBack = new Vector3(1.0f, -1.0f, -1.0f);

            vpc = new VertexPositionColor[36];

            // Front face
            vpc[0].Position = topLeftFront;
            vpc[1].Position = bottomLeftFront;
            vpc[2].Position = topRightFront;
            vpc[3].Position = bottomLeftFront;
            vpc[4].Position = bottomRightFront;
            vpc[5].Position = topRightFront;

            // Back face 
            vpc[6].Position = topLeftBack;
            vpc[7].Position = topRightBack;
            vpc[8].Position = bottomLeftBack;
            vpc[9].Position = bottomLeftBack;
            vpc[10].Position = topRightBack;
            vpc[11].Position = bottomRightBack;

            // Top face
            vpc[12].Position = topLeftFront;
            vpc[13].Position = topRightBack;
            vpc[14].Position = topLeftBack;
            vpc[15].Position = topLeftFront;
            vpc[16].Position = topRightFront;
            vpc[17].Position = topRightBack;

            // Bottom face 
            vpc[18].Position = bottomLeftFront;
            vpc[19].Position = bottomLeftBack;
            vpc[20].Position = bottomRightBack;
            vpc[21].Position = bottomLeftFront;
            vpc[22].Position = bottomRightBack;
            vpc[23].Position = bottomRightFront;

            // Left face
            vpc[24].Position = topLeftFront;
            vpc[25].Position = bottomLeftBack;
            vpc[26].Position = bottomLeftFront;
            vpc[27].Position = topLeftBack;
            vpc[28].Position = bottomLeftBack;
            vpc[29].Position = topLeftFront;

            // Right face 
            vpc[30].Position = topRightFront;
            vpc[31].Position = bottomRightFront;
            vpc[32].Position = bottomRightBack;
            vpc[33].Position = topRightBack;
            vpc[34].Position = topRightFront;
            vpc[35].Position = bottomRightBack;

            for (int i = 0; i < 36; i++)
            {
                vpc[i].Color = Color.Crimson;
            }

            #endregion

            vb.SetData<VertexPositionColor>(vpc);
        }

        public void Update()
        {
            MoveForward();
        }

        public void Draw(GameTime gTime)
        {
            GraphicsDevice device = graphics.GraphicsDevice;

            device.VertexDeclaration = decl;

            Matrix world;
            effect.Begin(SaveStateMode.None);

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();

                world = Matrix.CreateTranslation(position);

                device.Vertices[0].SetSource(vb, 0, VertexPositionColor.SizeInBytes);
                effect.Parameters["worldViewProjection"].SetValue(world * Camera.View * Camera.Projection);
                device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);

                pass.End();
            }
            effect.End();
        }

        #region Collision Detection
        /*
        void CheckForCollisions(ref Tank c1, ref Tank c2)
        {
            bool collision = false;

            // Check whether the bounding boxes of the two cubes intersect.
            if (c1.bb.Intersects(c2.bb))
            {
                collision = true;
                c2.ReverseVelocity();
            }

            // Check whether the moving cube intersects or has passed through the right plane.
            if (c1.bb.Intersects(new Plane(new Vector3(1, 0, 0), 10)) != PlaneIntersectionType.Front)
            {
                collision = true;
            }

            // Check whether the moving cube intersects or has passed through the left plane.
            if (c1.bb.Intersects(new Plane(new Vector3(-1, 0, 0), 10)) != PlaneIntersectionType.Front)
            {
                collision = true;
            }

            if (collision)
            {
                c1.Backup();
                c1.ReverseVelocity();
            }
        }
        */
        #endregion
    }
}
